var home=require("../../api/home");
var util=require("../../utils/util");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabListInst:[
      {
        id:1,
        url:'../../images/vip/1-1.png',
        curIndex:1
      },
      {
        id:2,
        url:'../../images/vip/2-2.png',
        curIndex:1
      },
      {
        id:3,
        url:'../../images/vip/3-3.png',
        curIndex:2
      },
      {
        id:4,
        url:'../../images/vip/4-4.png',
        curIndex:2
      },
    ],
    jxCourseList:[],
    currentIndex:1,
    tabIndex:0,
    showMore:false,
    class_list: [],
    sub_id:'',
    currentPage:1,
    pageSize:10,
    latitude:'',
    longitude:'',
    courseType:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    that.get_Home_classify_list()
    wx.getStorage({
      key: 'city',
      success:function(res){
        let info=res.data;
        that.setData({latitude:info.lat,longitude:info.lng})
      }
    })
  },

  // 进入加载数据
  get_Home_classify_list:function(){
    let that = this, arr1 = [], arr2 = []
    home.get_home_classify_list().then(res=>{
      if(res.data.code == '200'){
        const { data } = res.data
        let list = data
        list.forEach(item=>{
          if(item.position == 1){
            arr1.push(item)
          }else{
            arr2.push(item)
          }
          if(item.name === '学科辅导'){
            that.setData({sub_id:item.id})
          }
        })
        that.setData({class_list:arr2})
        that.get_all_child_id(arr2[0].id)
      }
    })
  },
    // 获取学科辅导分类
    get_cate_list:function(id){
      var that=this;
      var params={
        "parentId": id,
        "flagEnable": 1
      }
      home.get_cate_list(params).then(res=>{
        if(res.data.code==200){
          var list=res.data.data,
              arr=[];
              console.log(list);
          list.forEach(item=>{
            arr.push(item)
          })
          that.setData({class_list:arr})
          that.get_cour_list(list[0].id)
        }
      })
    },
  // 选择大分类
  handleTab:function(e){
    const { id } = e.currentTarget.dataset
    this.setData({courseType:id,class_list:[],jxCourseList:[],tabIndex:0,currentPage:1})
    var that = this
    if(id === 1 || id === 3 ){
      that.get_Home_classify_list()
      this.setData({currentIndex: 1})
    }else{
      that.get_cate_list(this.data.sub_id)
      this.setData({currentIndex: 2})
    }
  },
   // 选择小分类
  selTabLabel:function(e){
    const { index,id } = e.currentTarget.dataset
    this.setData({tabIndex:index,jxCourseList:[],currentPage:1})
    if(this.data.courseType !== 1 && this.data.courseType !== 3){
      this.get_cour_list(id)
    }
    this.get_all_child_id(id)
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })
  },

  // 获取小分类所有id
  get_all_child_id:function(id){
    const params ={
      parentId:id,
      flagEnable:1
    }
    home.get_cate_list(params).then(res=>{
      if(res.data.code == 200){
        let childListIds = []
        const { data } = res.data
        data.forEach(item=>{
          childListIds.push(item.id)
        })
        this.get_cour_list(childListIds.join(","))
      }
    })
  },
  // 获取课程列表
  get_cour_list:function(ids){
    let that = this,
        currentPage = that.data.currentPage,
        pageSize = that.data.pageSize
    wx.showLoading({ title: '加载中' })
    const params = {
      "currentPage": currentPage,
      "pageSize": pageSize,
      "status": 3,
      "categoryIds": ids,
      "lat": that.data.latitude,
      "lng": that.data.longitude
    }
    home.get_class_cour_list(params).then(res=>{
      if(res.data.code == 200){
        const { records } = res.data.data
        records.forEach(item=>{
          if(item.introducePictures){
            item.picture = item.introducePictures.split(',')
          }
          if(item.labelName){
            item.labelArray = item.labelName.split(',')
          }
          if(item.distance){
            item.space=item.distance>1000?(item.distance/1000).toFixed(2):item.distance;
          }
        })
        if(records.length == 0){
          util.page_show_toast('没有更多了',1500)
        }else{
          this.setData({jxCourseList:this.data.jxCourseList.concat(records)})
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    console.log(that.data.courseType);
    that.setData({currentPage:that.data.currentPage + 1})
    if(that.data.courseType == 1 || that.data.courseType == 3){
      that.get_Home_classify_list()
    }else{
      that.get_cate_list(that.data.sub_id)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})